import { GSprite } from "../../../../../../../com/mui/controls/GSprite";
import { GCheckBox } from "../../../../../../../com/mui/controls/GCheckBox";
import { EJewel } from "../../../../../../../Message/Public/EJewel";
import { ObjCreate } from "../../../../common/ObjCreate";
import { Language } from "../../../../../../../extend/language/Language";
import { GameDefConfig } from "../../../../../resource/GameDefConfig";
import { Global } from "../../../../../../../com/gengine/global/Global";
import { CallLater } from "../../../../../../common/net/CallLater";
import { DataEvent } from "../../../../../events/DataEvent";
import { GModuleEvent } from "../../../../../mvc/GModuleEvent";
import { HTMLUtil } from "../../../../../../../com/gengine/utils/HTMLUtil";
import { GlobalStyle } from "../../../../../../common/global/GlobalStyle";
import { ItemData } from "../../../../../resource/info/ItemData";
import { ItemsUtil } from "../../../../shortcuts/item/ItemsUtil";
import { JewelUtil } from "../../../../common/util/JewelUtil";
import { GComboBox } from "../../../../../../../com/mui/controls/GComboBox";
import { GTextFiled } from "../../../../../../../com/mui/controls/GTextFiled";
import { ScaleBitmap } from "../../../../../../../com/mui/display/ScaleBitmap";
import { Vector } from "../../../../../../../__AS3__/vec/Vector";
//class ComposeJewelSpecialSelecter
    
    export  class ComposeJewelSpecialSelecter extends GSprite
    {
        constructor()
        {
            super();
            this._cb_vec = new Vector.<GCheckBox>();
            this._cb_datas = [EJewel._EJewelLife, EJewel._EJewelPhysicalAttack, EJewel._EJewelMagicAttack, EJewel._EJewelPhysicalCrit, EJewel._EJewelMagicCrit, EJewel._EJewelPhysicalDefense, EJewel._EJewelMagicDefense, EJewel._EJewelJouk, EJewel._EJewelHit, EJewel._EJewelToughness, EJewel._EJewelPass];
            this.initUI();
            return;
        }

        /* internal  */initUI(): void
        {
            var loc1=0;
            var loc3=0;
            var loc4=null;
            this._comBtnSpecial = ObjCreate.createGComboBox(0, 0, 118, 30, null, this);
            this._comBtnSpecial.addEventListener(flash.events.MouseEvent.CLICK, this.onComBtnClickHandler);
            this._label = ObjCreate.createGTextField(Language.getString(61104), 5, 0, 118, 20, this);
            this._label.mouseEnabled = false;
            this._cb_sp = new GSprite();
            this._cb_sp.x = 0;
            this._cb_sp.y = 24;
            this._cb_bg = ObjCreate.createBg(0, 0, 118, 100, this._cb_sp);
            this._cb_all = ObjCreate.createCheckBox(Language.getString(61102), 5, 5, 100, 20, this._cb_sp);
            this._cb_all.selected = true;
            this._cb_all.addEventListener(flash.events.Event.CHANGE, this.onAllCbChangeHandler);
            var loc2=this._cb_datas.length;
            loc1 = 0;
            while (loc1 < loc2) 
            {
                loc3 = this._cb_datas[loc1];
                (loc4 = ObjCreate.createCheckBox(Language.getStringByParam(61106, GameDefConfig.instance.getEJewel(loc3).text1), 25, 27 + loc1 * 22, 100, 20, this._cb_sp)).addEventListener(flash.events.Event.CHANGE, this.onCbChangeHandler);
                loc4.selected = true;
                loc4.name = String(loc3);
                this._cb_vec.push(loc4);
                ++loc1;
            }
            this._cb_bg.height = 22 * (loc2 + 1) + 10;
            return;
        }

        /* internal  */onComBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._cb_sp.parent) 
            {
                this._cb_sp.parent.removeChild(this._cb_sp);
                Global.stage.removeEventListener(flash.events.MouseEvent.CLICK, this.onGlobalClickHandler);
            }
            else 
            {
                this.addChild(this._cb_sp);
                CallLater.addCallBack(this.addStageClickListener);
            }
            return;
        }

        /* internal  */addStageClickListener(): void
        {
            Global.stage.addEventListener(flash.events.MouseEvent.CLICK, this.onGlobalClickHandler);
            return;
        }

        /* internal  */onGlobalClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var colorUint: uint;

            var loc1;
            colorUint = 0;
            e = arg1;
            if (this._cb_sp.parent) 
            {
                colorUint = 0;
                try 
                {
                    colorUint = this._cb_bg.bitmapData.getPixel32(this._cb_bg.mouseX, this._cb_bg.mouseY);
                }
                catch (e)
                {
                    colorUint = 0;
                }
                if (colorUint <= 0) 
                {
                    this._cb_sp.parent.removeChild(this._cb_sp);
                    Global.stage.removeEventListener(flash.events.MouseEvent.CLICK, this.onGlobalClickHandler);
                }
            }
            else 
            {
                Global.stage.removeEventListener(flash.events.MouseEvent.CLICK, this.onGlobalClickHandler);
            }
            return;
        }

        /* internal  */onAllCbChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._cb_all.selected) 
            {
                this.updateSelect(true);
            }
            else 
            {
                this.updateSelect(false);
            }
            this.dispatchEvent(new DataEvent(GModuleEvent.ComposeJewelSelecterChange));
            return;
        }

        /* internal  */onCbChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.updateSelectAll();
            this.dispatchEvent(new DataEvent(GModuleEvent.ComposeJewelSelecterChange));
            return;
        }

        public updateSelect(arg1: Boolean): void
        {
            var loc1=0;
            var loc2=this._cb_vec.length;
            loc1 = 0;
            while (loc1 < loc2) 
            {
                this._cb_vec[loc1].selected = arg1;
                ++loc1;
            }
            this.updateLabel();
            return;
        }

        public updateSelectAll(): void
        {
            var loc2=0;
            var loc1=true;
            var loc3=this._cb_vec.length;
            loc2 = 0;
            while (loc2 < loc3) 
            {
                if (this._cb_vec[loc2].selected == false) 
                {
                    loc1 = false;
                    break;
                }
                ++loc2;
            }
            this._cb_all.selected = loc1;
            this.updateLabel();
            return;
        }

        /* internal  */updateLabel(): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=false;
            var loc4=0;
            var loc5=null;
            if (this._cb_all.selected) 
            {
                this._label.htmlText = HTMLUtil.addColor(Language.getString(61104), GlobalStyle.color1);
            }
            else 
            {
                loc2 = this._cb_vec.length;
                loc3 = false;
                loc1 = 0;
                while (loc1 < loc2) 
                {
                    if (this._cb_vec[loc1].selected) 
                    {
                        loc3 = true;
                        loc4 = this._cb_datas[loc1];
                        break;
                    }
                    ++loc1;
                }
                if (loc3) 
                {
                    loc5 = Language.getStringByParam(61106, GameDefConfig.instance.getEJewel(loc4).text1);
                    this._label.htmlText = HTMLUtil.addColor(Language.getStringByParam(61107, loc5), GlobalStyle.color1);
                }
                else 
                {
                    this._label.htmlText = HTMLUtil.addColor(Language.getString(61105), GlobalStyle.color1);
                }
            }
            return;
        }

        public checkJewel(arg1: ItemData): Boolean
        {
            var loc1=0;
            if (ItemsUtil.isJewel(arg1) && !JewelUtil.isNormalJewel(arg1)) 
            {
                if (this._cb_all.selected) 
                {
                    return true;
                }
                loc1 = 0;
                while (loc1 < this._cb_datas.length) 
                {
                    if (this._cb_datas[loc1] == arg1.type) 
                    {
                        return this._cb_vec[loc1].selected;
                    }
                    ++loc1;
                }
            }
            return false;
        }

        /* internal */ /* var */_comBtnSpecial: GComboBox;

        /* internal */ /* var */_label: GTextFiled;

        /* internal */ /* var */_cb_sp: GSprite;

        /* internal */ /* var */_cb_bg: ScaleBitmap;

        /* internal */ /* var */_cb_all: GCheckBox;

        /* internal */ /* var */_cb_vec: Vector.<GCheckBox>;

        /* internal */ /* var */_cb_datas: Array<any>;
    }
